<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 基金管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input placeholder="基金名称" v-model="query" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="getData(1)">搜索</el-button>
                <el-button type="success" icon="el-icon-circle-plus" @click="addVisible=true">发布基金</el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
            >
                <el-table-column type="index" :index="indexMethod" label="#"></el-table-column>
                <el-table-column prop="id" label="基金编号"></el-table-column>
                <el-table-column prop="managerName" label="基金经理"></el-table-column>
                <el-table-column prop="company" label="公司"></el-table-column>
                <el-table-column prop="createDate" label="注册时间"></el-table-column>
                <el-table-column prop="registeredCapital" label="资产规模"></el-table-column>
                <el-table-column prop="type" label="类型"></el-table-column>
                <el-table-column prop="name" label="基金名称"></el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :page-size="pageSize"
                        :total="pageTotal"
                        @current-change="getData"
                ></el-pagination>
            </div>
        </div>
        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="基金经理">
                    <el-input v-model="form.managerName"></el-input>
                </el-form-item>
                <el-form-item label="公司">
                    <el-input v-model="form.company"></el-input>
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-input :disabled="true"  v-model="form.createDate"></el-input>
                </el-form-item>
                <el-form-item label="资产">
                    <el-input v-model="form.registeredCapital"></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-select  v-model="form.type">
                        <el-option label="混合型" value="混合型"></el-option>
                        <el-option label="股票型" value="股票型"></el-option>
                        <el-option label="债券型" value="债券型"></el-option>
                        <el-option label="指数型" value="指数型"></el-option>
                    </el-select >
                </el-form-item>
                <el-form-item label="基金名称">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
        <!-- 新增弹框 -->
        <el-dialog title="编辑" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="基金经理">
                    <el-input v-model="addTable.managerName"></el-input>
                </el-form-item>
                <el-form-item label="公司">
                    <el-input v-model="addTable.company"></el-input>
                </el-form-item>
                <el-form-item label="创建时间">
                    <el-date-picker
                            v-model="addTable.createDate"
                            type="date"
                            placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="资产">
                    <el-input v-model="addTable.registeredCapital"></el-input>
                </el-form-item>
                <el-form-item label="类型">
                    <el-select  v-model="addTable.type">
                        <el-option label="混合型" value="混合型"></el-option>
                        <el-option label="股票型" value="股票型"></el-option>
                        <el-option label="债券型" value="债券型"></el-option>
                        <el-option label="指数型" value="指数型"></el-option>
                    </el-select >
                </el-form-item>
                <el-form-item label="基金名称">
                    <el-input v-model="addTable.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>

    export default {
        name: 'basetable',
        data() {
            return {
                query: '',
                tableData: [
                    {
                        id: 1,
                        managerName: '测试01',
                        company: '123',
                        createDate: '2019-10-1',
                        registeredCapital: 100000.00,
                        type: '指数型',
                        name: '基金名称'
                    },{
                        id: 1,
                        managerName: '测试02',
                        company: '123',
                        createDate: '2019-10-1',
                        registeredCapital: 100000.00,
                        type: '指数型',
                        name: '基金名称'
                    },{
                        id: 1,
                        managerName: '测试03',
                        company: '123',
                        createDate: '2019-10-1',
                        registeredCapital: 100000.00,
                        type: '指数型',
                        name: '基金名称'
                    },
                ],
                addTable:{
                    managerName: '测试03',
                    company: '123',
                    createDate: '2020-1-2',
                    registeredCapital: 100000.00,
                    type: '指数型',
                    name: '基金名称'
                },
                multipleSelection: [],
                delList: [],
                addVisible: false,
                editVisible: false,
                pageTotal: 3,
                pageSize: 10,
                currentPage: 1,
                form: {},
                idx: -1,
                id: -1,

            };
        },
        created() {
           this.getData(1);
        },
        methods: {
            //序号
            indexMethod(index) {
                const _index = index+1+(this.currentPage-1)*this.pageSize;
                return _index;
            },
            // 获取后台的模拟数据
            getData(cpage) {
                const _this = this;
                this.currentPage = cpage;
                this.$http.get(`/fundSearch/${cpage}/${this.pageSize}?query=${this.query}`).then(function (resp) {
                    _this.pageTotal = resp.data.total;
                    _this.tableData = resp.data.list;
                });
            },
            // 删除操作
            handleDelete(index, row) {
                const _this = this;
                // 二次确认删除
                this.$confirm('确定要删除基金:  '+row.name, '提示', {
                    type: 'warning'
                })
                    .then(() => {
                        this.$http.delete(`/fund/delete/${row.id}`).then(function (resp) {
                            if (resp.data == 'success'){
                                _this.$message.success('删除成功');
                                _this.tableData.splice(index, 1);
                            }
                        })
                    })
                    .catch(() => {});
            },
            // 编辑操作
            handleEdit(index, row) {
                this.idx = index;
                const _this = this;
                this.$http.get(`/fund/findOne/${row.id}`).then(function (resp) {
                    _this.form = resp.data;
                });
                this.editVisible = true;
            },
            // 保存编辑
            saveEdit() {
                const _this = this;
                this.$http.post('/fund/update',this.form).then(function (resp) {
                    if (resp.data == 'success'){
                        _this.$message.success(`修改第 ${_this.idx + 1} 行成功`);
                        _this.getData(1);
                    } else {
                        _this.$message.error(`修改第 ${_this.idx + 1} 行失败`);
                    }
                });
                this.editVisible = false;
            },
            // 保存新增
            saveAdd() {
                const _this = this;
                this.$http.post('/fund/add',this.addTable).then(function (resp) {
                    if (resp.data == 'success'){
                        _this.$message.success(`发布基金成功`);
                        _this.getData(1);
                    } else {
                        _this.$message.error(`发布基金失败`);
                    }
                });
                this.addVisible = false;
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
